<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="115px">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="项目名称:" prop="xmName">
            <el-input
              v-model="queryParams.xmName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目编号:" prop="xmBh">
            <el-input
              v-model="queryParams.xmBh"
              placeholder="请输入项目编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目状态:" prop="xmZt">
            <el-select v-model="queryParams.xmZt" placeholder="请选择项目状态" clearable>
              <el-option
                v-for="dict in dict.type.kylx_xmzt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="是否可高企认定:" prop="sfKGqrd">
            <el-select v-model="queryParams.sfKGqrd" placeholder="请选择是否可高企认定" clearable>
              <el-option
                v-for="dict in dict.type.sf"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-form :model="queryParams" size="mini" label-width="170px">
      <el-row :gutter="10" class="mb8" style="display: flex; justify-content: flex-end;margin-bottom: -8px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="kGqRd"
            :disabled="!YwJhByGqgl || (YwJhByGqgl !== true) || single"
          >可高企认定
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="bkGqRd"
            :disabled="!YwJhByGqgl || (YwJhByGqgl !== true) || single"
          >不可高企认定
          </el-button>
        </el-col>
        <el-col :span="1.5" style="margin-left: auto;">
          <el-form-item label="认定计划:" prop="xmZt">
            <vxe-select @change="getList" size="mini" v-model="queryParams.jhNd" placeholder="请选择认定计划">
              <vxe-option v-for="item in ywJhList" :key="item.nd" :value="item.nd"
                          :label="item.jhLb"></vxe-option>
            </vxe-select>
          </el-form-item>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              height="300" class="fixedTableTwo" v-loading="loading" :data="rdxmsxList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column show-overflow-tooltip label="计划年度" align="center">
        <template slot-scope="scope">
          <span>{{ queryParams.jhNd }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目年度" align="center" prop="xmNd"/>
      <el-table-column show-overflow-tooltip label="项目名称" align="center" prop="xmName"/>
      <el-table-column show-overflow-tooltip label="项目编号" align="center" prop="xmBh"/>
      <el-table-column show-overflow-tooltip label="负责人" align="center" prop="fzName"/>
      <el-table-column show-overflow-tooltip label="项目分类" align="center" prop="xmFl"/>
      <el-table-column show-overflow-tooltip label="承担单位" align="center" prop="cdDw"/>
      <el-table-column show-overflow-tooltip label="项目状态" align="center" prop="xmZt"/>
      <el-table-column show-overflow-tooltip label="项目总金额(万元)" align="center" prop="xmZe"/>
      <el-table-column show-overflow-tooltip label="第一年(元)" align="center" prop="dyn"/>
      <el-table-column show-overflow-tooltip label="第二年(元)" align="center" prop="den"/>
      <el-table-column show-overflow-tooltip label="第三年(元)" align="center" prop="dsn"/>
      <el-table-column show-overflow-tooltip label="是否可高企认定" align="center" prop="sfKGqrd"/>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {addRdjh, selectRdxmsxList, updateRdjh} from "@/api/gqgl/rdjh";
import {selectYwJh} from "@/api/xtgl/ywjh";

export default {
  name: "Rdxmsx", //认定项目筛选
  dicts: ['kylx_xmzt', 'year', 'sf'],
  data() {
    return {
      YwJhByGqgl: false,
      ywJhList: null, //业务计划列表
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xmNd: null,
        xmName: null,
        xmBh: null,
        xmZt: null,
        sfKGqrd: null,
        jhNd: null,
      },
      rdxmsxList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      selectedRow: null, // 保存当前选中的行

    }
  },
  created() {
    this.getListS()
  },
  methods: {
    //可高企认定
    kGqRd() {
      const row = this.selectedRow; // 获取当前选中的行
      console.log(row)
      let xmsbId = row.xmsbId
      let nd = this.queryParams.jhNd
      if (row.sfKGqrd == null) {
        let form = {
          rdjhId: null,
          xmsbId: xmsbId,
          nd: nd,
          sfKGqrd: '是',
        };
        addRdjh(form).then(response => {
          this.getList();
        });
      } else if (row.sfKGqrd == '否') {
        let form = {
          rdjhId: row.rdjhId,
          xmsbId: xmsbId,
          nd: nd,
          sfKGqrd: '是',
        };
        updateRdjh(form).then(response => {
          this.getList();
        });
      }
    },
    //不可高企认定
    bkGqRd() {
      const row = this.selectedRow; // 获取当前选中的行
      let xmsbId = row.xmsbId
      let nd = this.queryParams.jhNd
      if (row.sfKGqrd == null) {
        let form = {
          rdjhId: null,
          xmsbId: xmsbId,
          nd: nd,
          sfKGqrd: '否',
        };
        addRdjh(form).then(response => {
          this.getList();
        });
      } else if (row.sfKGqrd == '是') {
        let form = {
          rdjhId: row.rdjhId,
          xmsbId: xmsbId,
          nd: nd,
          sfKGqrd: '否',
        };
        updateRdjh(form).then(response => {
          this.getList();
        });
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.xmsbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //刚开始的查询
    getListS() {
      this.loading = true;
      let query = {
        pageNum: 1,
        pageSize: 50,
        ywSb: '平台资质',
        ywLx: '高企认定',
        zt: '启用'
      }
      selectYwJh(query).then(res => {
        this.ywJhList = res.rows;

        this.queryParams.jhNd = this.ywJhList[0].nd
        selectRdxmsxList(this.queryParams).then(response => {
          let query = {
            pageNum: 1,
            pageSize: 50,
            nd: this.queryParams.nd,
            ywSb: '平台资质',
            ywLx: '高企认定',
          }
          selectYwJh(query).then(res => {
            let ywjhList = res.rows;
            if (ywjhList[0].jhZt == '进行中') {
              this.YwJhByGqgl = true
            } else {
              this.YwJhByGqgl = false
            }

            this.rdxmsxList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        });
      });

    },
    getList() {
      this.loading = true;
      selectRdxmsxList(this.queryParams).then(response => {
        let query = {
          pageNum: 1,
          pageSize: 50,
          nd: this.queryParams.jhNd,
          ywSb: '平台资质',
          ywLx: '高企认定',
        }
        selectYwJh(query).then(res => {
          let ywjhList = res.rows;
          if (ywjhList[0].jhZt == '进行中') {
            this.YwJhByGqgl = true
          } else {
            this.YwJhByGqgl = false
          }
          this.rdxmsxList = response.rows
          this.total = res.total;
          this.loading = false;
        });
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },

  }
}
</script>

<style scoped>

</style>
